﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

/// <summary>
/// Summary description for RollupMetadata
/// </summary>
public class RollupMetadata
{

    bool _bAllCheckedTogether = false;

    int _nProds = 0;
    int _nUnits = 0;
    int _nNatls = 0;

    ArrayList _alProds = new ArrayList();
    ArrayList _alUnits = new ArrayList();
    ArrayList _alNatls = new ArrayList();

    bool _ByTracId;
    public bool ByTracId
    {
        get { return _ByTracId; }
        set { _ByTracId = value; }
    }

    public int Dimensions
    {
        get { return _nNatls * _nProds * _nUnits; }
    }

    public string[] _saLegendText;
    public string LegendText
    {
        get { return string.Join("*", _saLegendText); }

    }

    public void RefreshIndex(string psLnkBtn)
    {
        lnkbtn = psLnkBtn;
    }
    public void Refresh()
    {
        //Refresh all labels, filters and legend;
        CreateLegendText();
        CreateLabelsAndFilters();
    }

    private void CreateLabelsAndFilters()
    {
        _bAllCheckedTogether = (_nProds + _nUnits + _nNatls) == 0; //(ChlProducts.Items.Count + ChlNatl.Items.Count + ChlUnits.Items.Count);

        RefreshMetadata(ChlProducts, "PRODUCT", ref _ProductsLabel, ref _ProductsFilter);
        RefreshMetadata(ChlUnits, "DWELLING", ref _UnitsLabel, ref _UnitsFilter);
        RefreshMetadata(ChlNatl, "LOCATION", ref _NatlLabel, ref _NatlFilter);

        string[] saFilters = { ProductsFilter, NatlFilter, UnitsFilter };
        RollupFilter = "";
        for (int i = 0; i < saFilters.Length; i++)
        {
            if (saFilters[i].Length > 0)
                RollupFilter += (RollupFilter.Length > 0 ? " AND " : "") + saFilters[i];
        }
        RollupFilter = (RollupFilter.Length > 0 ? " AND " : "") + RollupFilter;

        RefreshDate();
    }

    public void RefreshDate()
    {
        string[] saLabels = { NatlLabel, UnitsLabel, ProductsLabel };
        SalesMonth = (new Utility()).Last3Months().Table.Rows[MonthsPrior].ItemArray[0].ToString();
        RollupLabel = SalesMonth + (" " + String.Join(" ", saLabels)).TrimEnd() + " Net Sales";

        DailyLabel = SalesMonth + " Daily Net Sales";
        MonthlyLabel = (String.Join(" ", saLabels)).Trim() + " Net Sales";

        if (HttpContext.Current.Session["SalesDate"]!=null)
            ReportDate = ((DateTime)HttpContext.Current.Session["SalesDate"]).AddMonths(-MonthsPrior);
        else
            ReportDate = DateTime.Today.AddDays(-1).AddMonths(-MonthsPrior);

        DateFilter = String.Format(" AND MN='{0}' AND YR='{1}'", ReportDate.Month, ReportDate.Year);

        if (MonthsPrior < 12)
            PriorMonthDateFilter = String.Format(" AND MN='{0}' AND YR='{1}'", ReportDate.AddMonths(-1).Month, ReportDate.AddMonths(-1).Year);
    }

    public void RefreshDimensions(string[] psaDims)
    {
        for (int i = 0; i < ChlProducts.Items.Count; i++) ChlProducts.Items[i].Selected = false;
        for (int i = 0; i < ChlNatl.Items.Count; i++) ChlNatl.Items[i].Selected = false;
        for (int i = 0; i < ChlUnits.Items.Count; i++) ChlUnits.Items[i].Selected = false;

        ListItem li;
        for (int i = 0; i < psaDims.Length; i++)
        {
            li = ChlProducts.Items.FindByValue(psaDims[i]); if (li != null) li.Selected = true;
            li = ChlNatl.Items.FindByValue(psaDims[i]); if (li != null) li.Selected = true;
            li = ChlUnits.Items.FindByValue(psaDims[i]); if (li != null) li.Selected = true;
        }

        Refresh();
    }

    public void CreateLegendText()
    {
        _nProds = 0;
        _nUnits = 0;
        _nNatls = 0;

        _alProds = new ArrayList();
        _alUnits = new ArrayList();
        _alNatls = new ArrayList(); 
        
        for (int i = 0; i < ChlProducts.Items.Count; i++)
        {
            if (ChlProducts.Items[i].Selected)
            {
                _nProds += 1;
                _alProds.Add(ChlProducts.Items[i].Text);
            }
        }
        if (_nProds == 0) { _nProds = 1; _alProds.Add(""); }

        for (int i = 0; i < ChlUnits.Items.Count; i++)
        {
            if (ChlUnits.Items[i].Selected)
            {
                _nUnits += 1;
                _alUnits.Add(ChlUnits.Items[i].Text);
            }
        }
        if (_nUnits == 0) { _nUnits = 1; _alUnits.Add(""); }

        for (int i = 0; i < ChlNatl.Items.Count; i++)
        {
            if (ChlNatl.Items[i].Selected)
            {
                _nNatls += 1;
                _alNatls.Add(ChlNatl.Items[i].Text);
            }

        }
        if (_nNatls == 0) { _nNatls = 1; _alNatls.Add(""); }


        _saLegendText = new string[Dimensions];

        for (int i = 0; i < _nProds; i++)
        {
            for (int j = 0; j < _nNatls; j++)
            {

                for (int k = 0; k < _nUnits; k++)
                {
                    int n = k + j * _nUnits + i * _nNatls * _nUnits;
                    _saLegendText[n] =
                        (_alNatls[j] + " ").TrimStart() + (_alUnits[k] + " ").TrimStart() + _alProds[i];
                }
            }
        }
    }

    private void RefreshMetadata(CheckBoxList pChl, string psFieldName, ref string psLabel, ref string psFilter)
    {
        int n = 0;
        for (int i = 0; i < pChl.Items.Count; i++) n += pChl.Items[i].Selected ? 1 : 0;

        if (_bAllCheckedTogether == true)
        {
            psFilter = psFieldName + " IS NULL";
            psLabel = "";
        }

        //else
        // if (n == pChl.Items.Count)
        //{
        //    psLabel = AndLabel(pChl);
        //    psFilter = OrFilter(pChl, psFieldName);
        //}

        else if (n == 0)
        {
            psLabel = "";
            psFilter = psFieldName + " IS NULL"; // psFieldName + "=" + psFieldName;
        }

        else
        {
            psFilter = OrFilter(pChl, psFieldName);
            psLabel = AndLabel(pChl);
        }
    }

    private string AndLabel(CheckBoxList pChl)
    {
        string sLabel = "";
        for (int i = 0; i < pChl.Items.Count; i++)
        {
            if (pChl.Items[i].Selected == true)
                sLabel += (sLabel.Length > 0 ? " & " : "") + pChl.Items[i].Value;
        }
        return sLabel;
    }

    private string OrFilter(CheckBoxList pChl, string psFieldName)
    {
        string sFilter = "";
        for (int i = 0; i < pChl.Items.Count; i++)
        {
            if (pChl.Items[i].Selected == true)
                sFilter += (sFilter.Length > 0 ? " OR " : "") + string.Format("{0}='{1}'", psFieldName, pChl.Items[i].Value);
        }
        return "(" + sFilter + ")";
    }

    private RadioButtonList _OplDisplay;

    public RadioButtonList OplDisplay
    {
        get { return _OplDisplay; }
        set { _OplDisplay = value; }
    }

    public bool DisplayBoth()
    {
        return OplDisplay.Items[0].Selected;
    }

    public bool DisplayDaily()
    {
        return OplDisplay.Items[1].Selected;
    }

    public bool DisplayMonthly()
    {
        return OplDisplay.Items[2].Selected == true;
    }


    private string _PriorMonthDateFilter=string.Empty;
    public string PriorMonthDateFilter
    {
        get { return _PriorMonthDateFilter; }
        set { _PriorMonthDateFilter = value; }
    }

    private string _DateFilter;
    public string DateFilter
    {
        get { return _DateFilter; }
        set { _DateFilter = value; }
    }

    private CheckBox _chkStacked;
    public CheckBox ChkStacked
    {
        get { return _chkStacked; }
        set { _chkStacked = value; }
    }

    private CheckBox _chkShowTables;
    public CheckBox chkShowTables
    {
        get { return _chkShowTables; }
        set { _chkShowTables = value; }
    }

    private CheckBox _chkShowSummary;
    public CheckBox chkShowSummary
    {
        get { return _chkShowSummary; }
        set { _chkShowSummary = value; }
    }

    private CheckBoxList _chlProducts;
    public CheckBoxList ChlProducts
    {
        get { return _chlProducts; }
        set
        {
            _chlProducts = value;

        }
    }

    private CheckBoxList _chlUnits;
    public CheckBoxList ChlUnits
    {
        get { return _chlUnits; }
        set
        {
            _chlUnits = value;
        }
    }

    private CheckBoxList _chlNatl;
    public CheckBoxList ChlNatl
    {
        get { return _chlNatl; }
        set { _chlNatl = value; }
    }

    private CheckBox _chkSyncDaily;

    public CheckBox ChkSyncDaily
    {
        get { return _chkSyncDaily; }
        set { _chkSyncDaily = value; }
    }

    private CheckBox _chkShowZeroDays;

    public CheckBox ChkShowZeroDays
    {
        get { return _chkShowZeroDays; }
        set { _chkShowZeroDays = value; }
    }

    private string[] _Products;
    public string[] Products
    {
        get { return _Products; }
        set { _Products = value; }
    }

    private string[] _Units;
    public string[] Units
    {
        get { return _Units; }
        set { _Units = value; }
    }
    private string[] _Natl;

    public string[] Natl
    {
        get { return _Natl; }
        set { _Natl = value; }
    }

    private string _SalesMonth;

    public string SalesMonth
    {
        get { return _SalesMonth; }
        set { _SalesMonth = value; }
    }

    private string _DailyLabel;
    public string DailyLabel
    {
        get { return _DailyLabel; }
        set { _DailyLabel = value; }
    }

    private string _MonthlyLabel;
    public string MonthlyLabel
    {
        get { return _MonthlyLabel; }
        set { _MonthlyLabel = value; }
    }

    private int _MonthsPrior;
    public int MonthsPrior
    {
        get { return _MonthsPrior; }
        set { _MonthsPrior = value; }
    }

    private string _ProductsLabel;
    public string ProductsLabel
    {
        get { return _ProductsLabel; }
        set { _ProductsLabel = value; }
    }

    private string _ProductsFilter;
    public string ProductsFilter
    {
        get { return _ProductsFilter; }
        set { _ProductsFilter = value; }
    }

    private string _UnitsLabel;
    public string UnitsLabel
    {
        get { return _UnitsLabel; }
        set { _UnitsLabel = value; }
    }

    private string _UnitsFilter;
    public string UnitsFilter
    {
        get { return _UnitsFilter; }
        set { _UnitsFilter = value; }
    }

    private string _NatlLabel;
    public string NatlLabel
    {
        get { return _NatlLabel; }
        set { _NatlLabel = value; }
    }

    private string _NatlFilter;
    public string NatlFilter
    {
        get { return _NatlFilter; }
        set { _NatlFilter = value; }
    }

    private string _RollupLabel;
    public string RollupLabel
    {
        get { return _RollupLabel; }
        set { _RollupLabel = value; }
    }

    private string _RollupFilter;
    public string RollupFilter
    {
        get { return _RollupFilter; }
        set { _RollupFilter = value; }
    }

    private string _ChartLegend;
    public string ChartLegend
    {
        get { return _ChartLegend; }
        set { _ChartLegend = value; }
    }

    private string _DailyChartStyle = "";
    public string DailyChartStyle
    {
        get { return _DailyChartStyle; }
        set { _DailyChartStyle = value; }
    }

    private DateTime _ReportDate;
    public DateTime ReportDate
    {
        get { return _ReportDate; }
        set { _ReportDate = value; }
    }

    private string _lnkbtn;
    public string lnkbtn
    {
        get { return _lnkbtn; }
        set { _lnkbtn = value; }
    }

    public RollupMetadata()
    {
        //
        // TODO: Add constructor logic here
        //
    }

}